package filemaker;

import java.io.File;
import java.util.Vector;

/**
 *
 * @author Sean
 */
public class MakeFile {

    public File CreateAFile(String folderName, String fileName, boolean overWrite) throws Exception {

        //test
        if (folderName != null) {

            if (folderName.trim().length() > 0) {

                if (!folderName.endsWith("\\")) {
                    folderName = folderName + "\\";
                }

                // Check if the directory exists
                // And if it doesn't, create it.
                File dir = new File(folderName);
                if (!dir.isDirectory()) {
                    dir.mkdir();
                }

            }
        } else {
            folderName = "";
        }

        File file = new File(folderName + fileName);
        if (file.exists()) {
            if (overWrite) {
                file.delete();
            } else // todo - need to check this.
            {
                return file;
            }
        }

        file.createNewFile();

        return file;
    }

    public Vector<File> CreateRandomFiles(String directory, int numberOfFiles) throws Exception {

        Vector<File> files = new Vector<File>();

        for (int n = 1; n <= numberOfFiles; n++) {

            String fileName = "";
            for (int letter = 1; letter <= 10; letter++) {
                fileName += RandomChar();
            }

            File file = CreateAFile(directory, fileName + ".txt", true);
            files.add(file);
        }
        return files;

    }

    private char RandomChar() {
        int character = 97 + (int) (Math.random() * 26);
        return (char) character;
    }
}
